package com.education.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.education.commonutils.ResultAjax;
import com.education.servicebase.config.exceptionjandler.GuliException;
import com.education.serviceedu.entity.EduTeacher;
import com.education.serviceedu.entity.vo.TeacherQuery;
import com.education.serviceedu.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zwr
 * @since 2021-05-23
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/serviceedu/teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 1、查询讲师表所有数据
     *
     * @return
     */
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public ResultAjax findAllTeacher() {
        List<EduTeacher> teacherList = eduTeacherService.list(null);
        return ResultAjax.success().data("items", teacherList);
    }

    /**
     * 2、逻辑删除讲师方法
     *
     * @param id 讲师ID
     * @return
     */
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public ResultAjax removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true)
                                    @PathVariable String id) {
        boolean flag = eduTeacherService.removeById(id);
        if (flag) {
            return ResultAjax.success();
        } else {
            return ResultAjax.error();
        }
    }

    /**
     * 3 分页查询讲师的方法
     *
     * @param current 当前页
     * @param limit   每页记录数
     * @return
     */
    @ApiOperation(value = "分页查询讲师列表")
    @GetMapping("pageTeacher/{current}/{limit}")
    public ResultAjax pageListTeacher(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
                                      @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        try {
            int i = 10 / 0;
        } catch (Exception e) {
            throw new GuliException(201, "执行了自定义异常处理....");
        }
        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        eduTeacherService.page(pageTeacher, null);
        //总记录数
        long total = pageTeacher.getTotal();
        //数据list集合
        List<EduTeacher> records = pageTeacher.getRecords();
        return ResultAjax.success().data("total", total).data("row", records);
    }

    /**
     * 条件查询带分页的方法
     *
     * @param current
     * @param limit
     * @param teacherQuery
     * @return
     */
    @ApiOperation(value = "条件查询讲师列表带分页")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public ResultAjax pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                           @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        eduTeacherService.page(pageTeacher, wrapper);

        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return ResultAjax.success().data("total", total).data("rows", records);
    }

    /**
     * 添加讲师
     *
     * @param eduTeacher
     * @return
     */
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public ResultAjax addTeacher(@ApiParam(name = "eduTeacher", value = "讲师信息", required = true) @RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        if (save) {
            return ResultAjax.success();
        } else {
            return ResultAjax.error();
        }
    }

    /**
     * 根据讲师id进行查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据讲师id进行查询")
    @GetMapping("getTeacher/{id}")
    public ResultAjax getTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return ResultAjax.success().data("teacher", eduTeacher);
    }


    @ApiOperation(value = "根据ID修改讲师")
    @PostMapping("updateTeacher")
    public ResultAjax updateById(@ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher) {
        boolean flag = eduTeacherService.updateById(teacher);
       if(flag){
           return ResultAjax.success();
       }else{
           return ResultAjax.error();
       }
    }
}

